package com.auxgroup.hr.front.service.hireperson.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.Constant;
import com.auxgroup.hr.front.commons.config.BusinessConfig;
import com.auxgroup.hr.front.commons.utils.File2Byte;
import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.dao.domain.PsRsHirePhoto;
import com.auxgroup.hr.front.dao.domain.PsRsHirePhotoPrimary;
import com.auxgroup.hr.front.dao.repository.PsRsHirePhotoRepository;
import com.auxgroup.hr.front.service.hireperson.HirePhotoService;

/** 
 * @author lvliuhao
 * @version 创建时间：180523 
 * 入职人员头像service
 */
@Service("hirePhotoService")
public class HirePhotoServiceImpl implements HirePhotoService {
	
	@Autowired
	private  PsRsHirePhotoRepository psRsHirePhotoRepository;
	
	@Autowired
	private BusinessConfig businessConfig;


	@Override
	public PsRsHirePhoto getHirePhoto(PsRsHirePhotoPrimary hirePrimary) {
		return psRsHirePhotoRepository.findOne(hirePrimary);
	}

	
	@Override
	public String buildEmplPhoto(PsRsHirePhoto photo) {
		StringBuilder jpgPath = new StringBuilder(businessConfig.getEmplePhotoResourceLocation());
		StringBuilder jpgName = new StringBuilder(photo.getRsHireId());
		jpgName.append(photo.getPhotoSizename());
		jpgName.append(".jpg");
		jpgPath.append(jpgName);
		Boolean isSucc = File2Byte.byte2image(photo.getEmployeePhoto(), jpgPath.toString(),true);
		if(isSucc){
			return Constant.EMPLPHOTO_STATIC_PATH+jpgName.toString();
		}
		return null;
	}

	@Override
	public PsRsHirePhoto save(PsRsHirePhoto photo) {
		StringBuilder ever = new StringBuilder();
	    for(int i=0;i<10;i++ ) {
	    	ever.append((int)(10*(Math.random())));
	    }
		photo.setPsimagever(new BigDecimal(ever.toString()));
		ObjectFieldUtils.handleNullValue(photo);
		return psRsHirePhotoRepository.save(photo);
	}


	


	
	
	
}
